package org.huangxiaozhe.adventure;
import java.util.HashSet;
import java.util.Iterator;

import org.huangxiaozhe.adventure.common.ConstantUtil;
import org.huangxiaozhe.adventure.common.MathTool;
import org.huangxiaozhe.adventure.common.Parabola;

import com.triplefstudio.adventure.R;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.widget.Toast;
/**
 * ����Ϊ����ķ�װ��
 */
public class Pig {
	public static final String tag = "finger";
	
	private float x;	//����ĺ����
	private float y;	//����������
	
	int life;//����
	public int currentTool = 0;	//��ǰ�?ʹ�ù���
	public boolean canChangeTool = true;	//�Ƿ���Ի�����
	
	Bitmap bitmapPig;		//��ǰ�����ͼƬ
	Bitmap bitmapNone;		//��ʹ�ù��ߵ���
	Bitmap bitmapTank;		//ʹ��̹�˹��ߵ���
	Bitmap bitmapBoat;		//ʹ�ô�����
	Bitmap bitmapPlane; 	//ʹ�÷ɻ����
	Bitmap bitmapCatapult;	//ʹ�õ���
	
	GameView gameView;		//GameView������
	//private int span = 10;	//������һ��������
	private float planeLevel;	//��ǰ���и߶�
	private float planePercent = 0.3f;
	
	HashSet<Bullet> bullets = new HashSet<Bullet>();	//̹�˴�����ӵ�
	float fingerX = 0;		//��ָ�ƶ�����ʱ�ĺ����
	float fingerY = 0;		//��ָ�ƶ�����ʱ�������
	public boolean fingerDrag = false;	//���ڱ�ǵ�ǰ�Ƿ����ƶ�����
	
	float catapultX = 0;	//����ͼƬ����ʱ�ĺ����
	float catapultY = 0;	//����ͼƬ����ʱ�������
	static final int catapultOffset = 40;	//����ͼƬ����ʱ��������ͨ�������ƫ����
	float catapultCx;		//��������ʹ��ʱ��Բ�ĺ����
	float catapultCy;		//��������ʹ��ʱ��Բ�������
	static final int catapultRadiusIn = 20;		//ʹ�õ���ʱ�ƶ�ʱ���ľ�Բ�ĵ���С����
	static final int catapultRadiusOut = 60;	//ʹ�õ���ʱ�ƶ�ʱ��Ч��Χ��������
	
	public Pig(int x, int y, int tool,int life, GameView gameView){
		this.gameView = gameView;
		this.x = x;
		this.y = y; 
		this.currentTool = tool;
		this.life = life;
		initBitmap();
	}
	
	public void initBitmap(){
		bitmapNone = BitmapFactory.decodeResource(gameView.getResources(), R.drawable.pig1);
		bitmapTank = BitmapFactory.decodeResource(gameView.getResources(), R.drawable.tank);
		bitmapBoat = BitmapFactory.decodeResource(gameView.getResources(), R.drawable.boat);
		bitmapPlane = BitmapFactory.decodeResource(gameView.getResources(), R.drawable.plane);
		bitmapCatapult = BitmapFactory.decodeResource(gameView.getResources(), R.drawable.catapult);
		useTool(0);
	}
	public void draw(Canvas canvas){
		Paint paint = new Paint();
		paint.setColor(Color.BLACK);
		
		if(currentTool == 0 ||
				currentTool == 2 ||
				currentTool == 3 ){
			float horizon = gameView.road.getHorizon(x, x + ConstantUtil.pigWidth)- ConstantUtil.pigHeight;
			canvas.drawLine(x, horizon, x + ConstantUtil.pigWidth, horizon, paint);
			y = horizon ;
			
			//������
			canvas.drawBitmap(bitmapPig, x, y, paint);
			//ȡ������������������Ӧ��·��������
			paint.setColor(Color.BLACK);
			float temp1 = gameView.road.getY(x);// - y - ConstantUtil.planeHeight;
			float temp2 = gameView.road.getY(x + ConstantUtil.pigWidth);// - y - ConstantUtil.planeHeight;
			canvas.drawCircle(x, temp1, 3, paint);
			canvas.drawCircle(x+ConstantUtil.pigWidth, temp2, 3, paint);
			//���Ʒ����ĸ�����ĵ�
			paint.setColor(Color.BLUE);
			canvas.drawCircle(x, y, 3, paint);
			canvas.drawCircle(x, y + ConstantUtil.pigHeight, 3, paint);
			canvas.drawCircle(x + ConstantUtil.pigWidth, y, 3, paint);
			canvas.drawCircle(x + ConstantUtil.pigWidth, y + ConstantUtil.pigHeight, 3, paint);
		}
		else if(currentTool == 4){
			y = planeLevel;
			//������
			canvas.drawBitmap(bitmapPig, x, y, paint);
		}
		else if(currentTool == 1 ){
			paint.setColor(Color.GRAY);
			canvas.drawBitmap(bitmapCatapult, catapultX, catapultY, paint);
			canvas.drawLine(catapultX + 3, catapultY + 3, fingerX, fingerY, paint);
			canvas.drawLine(catapultX + bitmapCatapult.getWidth() - 3, catapultY + 5, fingerX, fingerY, paint);
			canvas.drawBitmap(bitmapNone, fingerX - bitmapNone.getWidth()/2, fingerY - bitmapNone.getHeight()/2, paint);
		}
		else if(currentTool == 5){
			if(fingerX - bitmapNone.getWidth()/2 < x){
				canvas.drawBitmap(bitmapCatapult, catapultX, catapultY, paint);
				canvas.drawBitmap(bitmapNone, fingerX - bitmapNone.getWidth()/2, fingerY - bitmapNone.getHeight()/2, paint);
			}
			else{
				canvas.drawBitmap(bitmapCatapult, catapultX, catapultY, paint);
				canvas.drawBitmap(bitmapPig, x, y, paint);
			}
		}
		
		//�����ӵ�
		synchronized(bullets){
			for(Bullet bullet:bullets){
				bullet.draw(canvas);
			}
		}
	}
	
	public boolean isInclude(float px, float py){
		if(px >= x && px <=  x + ConstantUtil.pigHeight
				&& py >= y && py <= y + ConstantUtil.pigWidth){
			return true;
		}
		else{
			return false;
		}
	}
	
	public void useTool(int index){
		if(!canChangeTool){
			return;
		}
		Log.d(tag, "change to tool " + index);
		this.currentTool = index;
		switch(currentTool){
		case 0:
			this.bitmapPig = bitmapNone;
			ConstantUtil.pigY = ConstantUtil.screenHeight * 4 / 5 - ConstantUtil.pigHeight;
			break;
		case 1:
			gameView.canStart = false;
			gameView.Stop();
			this.catapultX = x + catapultOffset;
			this.catapultY = gameView.road.getY(x + catapultOffset + bitmapCatapult.getWidth()/2) - bitmapCatapult.getHeight();
			this.catapultCx = catapultX + bitmapCatapult.getWidth()/2;
			this.catapultCy = catapultY + 3;
			this.fingerX = catapultCx;
			this.fingerY = catapultCy;
			break;
		case 2:
			this.bitmapPig =  bitmapBoat;
			ConstantUtil.pigY = ConstantUtil.screenHeight * 4 / 5 - ConstantUtil.pigHeight;
			break;
		case 3:
			this.bitmapPig = bitmapTank;
			ConstantUtil.pigY = ConstantUtil.screenHeight * 4 / 5 - ConstantUtil.pigHeight;
			break;
		case 4:
			planeLevel = y + bitmapPig.getHeight() - bitmapPlane.getHeight();
			this.bitmapPig = bitmapPlane;
			break;
		case 5:
			this.bitmapPig = bitmapNone;
			break;
		}
		ConstantUtil.pigHeight = bitmapPig.getHeight();
		ConstantUtil.pigWidth = bitmapPig.getWidth();
	}
	
	public void planeGo(float distance){
		
		if(currentTool == 4){
			float temp = planeLevel + distance * planePercent;
			 if(temp < 0){
				 planeLevel = 0;
			 }
			 else if(temp > ConstantUtil.screenHeight - bitmapPig.getWidth()){
				 planeLevel = ConstantUtil.screenHeight - bitmapPig.getWidth();
			 }
			 else{
				 planeLevel = temp;
			 }
		}
	}
	
	public int  getCurTool(){
		return currentTool;
	}

	public void shoot(float ex, float ey) {
		if(currentTool == 3){
			if(ex > x+ConstantUtil.pigWidth){
				Bullet bullet = new Bullet(x + ConstantUtil.pigWidth, y + 20, ex, ey);
				synchronized(bullets){
					bullets.add(bullet);
				}
			}
		}
	}

	public void Move() {
		if(currentTool == 3){
			synchronized(bullets){
				
				Iterator<Bullet> it=bullets.iterator(); 
				while(it.hasNext()){
					Bullet bullet = (Bullet) it.next();
					if(!bullet.Move()){
						it.remove();
					}
				}
			}
		}
		else if(currentTool == 5){
			if(fingerX - bitmapNone.getWidth()/2 >= x){
				gameView.canStart = true;
				gameView.Start();
				catapultX -= ConstantUtil.roadSpan;
				float horizon = gameView.road.getHorizon(x, x + ConstantUtil.pigWidth)- ConstantUtil.pigHeight;
				float inc = Parabola.next();
				y -= inc;
				Log.d(tag, "inc:" + inc + " y>horizon " + (y>horizon));
				if(y > horizon && inc < 0){
					canChangeTool = true;
					useTool(0);
					y = horizon;
				}
			}
			else{
				fingerX += ConstantUtil.roadSpan;
				fingerY -= Parabola.t * Parabola.vy;
				y = fingerY - bitmapNone.getHeight()/2;
			}
			
		}
	}

	public void downFinger(float x2, float y2) {
		if(currentTool == 1){
			if(!fingerDrag && MathTool.getDistance(x2, y2, catapultCx, catapultCy) <= catapultRadiusIn){
				Log.d(tag, "start drag");
				fingerDrag = true;
			}
		}
	}

	public void moveFinger(float x2, float y2) {
		if(currentTool == 1){
			if(fingerDrag){
				float length = MathTool.getDistance(x2, y2, catapultCx, catapultCy);
				if(length <= catapultRadiusOut){
					Log.d(tag, "finger move in radius");
					this.fingerX = x2;
					this.fingerY = y2;
				}
				else{
					Log.d(tag, "finger move out radius");
					if(y2 <= catapultCy || x2 >= catapultCx - 25){
						fingerX = catapultCx;
						fingerY = catapultCy;
					}
					else{
						fingerX = catapultCx + (x2 - catapultCx) * catapultRadiusOut / length; 
						fingerY = catapultCy + (y2 - catapultCy) * catapultRadiusOut / length; 
					}
				}
			}
		}
		
	}

	public void upFinger(float x2, float y2) {
		if(currentTool == 1){
			if(fingerDrag){
				if(MathTool.getDistance(catapultCx, catapultCy, fingerX, fingerY) > catapultRadiusIn){
					//�ͷ�ʱλ�þ��������㹻Զ
					Parabola.set(catapultCx - fingerX, fingerY - catapultCy);
					canChangeTool = true;
					useTool(5);
					canChangeTool = false;
				}
				Log.d(tag, "stop drag");
				fingerDrag = false;
			}
		}
	}
}